package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.ComplaintManagementMapper;
import com.one.group.model.dto.ComplaintManagementAddDto;
import com.one.group.model.dto.ComplaintManagementQueryDto;
import com.one.group.model.dto.ComplaintManagementUpdateDto;
import com.one.group.model.dto.UpdateComplaintStatusDto;
import com.one.group.model.entity.ComplaintManagement;
import com.one.group.model.vo.ComplaintManagementListVo;
import com.one.group.service.ComplaintManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 投诉管理服务实现类
 * @author
 * @date
 * @description
 */
@Slf4j
@Service
public class ComplaintManagementServiceImpl extends ServiceImpl<ComplaintManagementMapper, ComplaintManagement> implements ComplaintManagementService {

    @Autowired
    private ComplaintManagementMapper complaintManagementMapper;

    @Override
    public boolean addComplaintManagement(ComplaintManagementAddDto addDto) {
        ComplaintManagement complaintManagement = new ComplaintManagement();
        BeanUtil.copyProperties(addDto, complaintManagement);
        if (complaintManagement.getTenantId() == null) {
            complaintManagement.setTenantId(Convert.toLong(StpUtil.getExtra("tenant_id")));
        }
        log.debug("添加投诉记录: {}", complaintManagement);
        return complaintManagementMapper.insertSelective(complaintManagement) > 0;
    }

    @Override
    public List<ComplaintManagementListVo> findComplaintManagementByQueryDto(ComplaintManagementQueryDto queryDto) {

        if (queryDto.getTenantId() == null) {
            queryDto.setTenantId(Convert.toLong(StpUtil.getExtra("tenant_id")));
        }
        log.debug("查询投诉记录: {}", queryDto);
        return complaintManagementMapper.findComplaintManagementByQueryDto(queryDto);
    }


    @Override
    public boolean updateComplaintManagement(ComplaintManagementUpdateDto updateDto) {
        ComplaintManagement complaintManagement = new ComplaintManagement();
        BeanUtil.copyProperties(updateDto, complaintManagement);
        log.debug("更新投诉记录: {}", complaintManagement);
        return complaintManagementMapper.update(complaintManagement) > 0;
    }

    @Override
    public boolean deleteComplaintManagement(Long id) {
        // 使用逻辑删除
        ComplaintManagement complaintManagement = new ComplaintManagement();
        complaintManagement.setId(id);
        complaintManagement.setIsDeleted(1);
        log.debug("删除投诉记录: {}", id);
        return complaintManagementMapper.update(complaintManagement) > 0;
    }

    @Override
    public ComplaintManagement getComplaintManagementById(Long id) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .eq(ComplaintManagement::getId, id)
                .eq(ComplaintManagement::getIsDeleted, 0);
        return complaintManagementMapper.selectOneByQuery(queryWrapper);
    }
    
    @Override
    public boolean acceptComplaint(UpdateComplaintStatusDto updateComplaintStatusDto) {

        updateComplaintStatusDto.setTenantId(Convert.toLong(StpUtil.getExtra("tenant_id")));
        log.debug("受理投诉: {}", updateComplaintStatusDto);
        return complaintManagementMapper.updateComplaintStatus(updateComplaintStatusDto);
    }
}